package cn.FagDegrn.chatbot.api.domain.zsxq.service;

import cn.FagDegrn.chatbot.api.domain.zsxq.IzsxqApi;
import cn.FagDegrn.chatbot.api.domain.zsxq.model.aggregates.UnansweredAggregates;
import cn.FagDegrn.chatbot.api.domain.zsxq.model.req.AnswerReq;
import cn.FagDegrn.chatbot.api.domain.zsxq.model.req.ReqData;
import cn.FagDegrn.chatbot.api.domain.zsxq.model.res.AnswerRes;
import com.alibaba.fastjson.JSON;
import net.sf.json.JSONObject;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;
@Service
public class ZsxqApi implements IzsxqApi {
    private Logger logger= LoggerFactory.getLogger(ZsxqApi.class);

    @Override
    public UnansweredAggregates queryUnansweredQuestionTopicId(String groupId, String cookie) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        HttpGet get = new HttpGet("https://api.zsxq.com/v2/groups/"+groupId+"/topics?scope=unanswered_questions&count=20");

        get.addHeader("cookie",cookie);
        get.addHeader("Content-Type","application/json, text/plain, */*,charset=utf8");

        CloseableHttpResponse response = httpClient.execute(get);

        if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
            String jsonStr = EntityUtils.toString(response.getEntity());
            logger.info("拉去问题的数据:groupid:{},jsonstr:{}",groupId,jsonStr);
            return JSON.parseObject(jsonStr, UnansweredAggregates.class);
        }else{
            throw new RuntimeException("queryUnansweredQuestionTopicId err code is "+response.getStatusLine().getStatusCode());
        }

    }

    @Override
    public boolean answer(String groupId, String cookie, String topicId, String text, boolean silenced) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        HttpPost post = new HttpPost("https://api.zsxq.com/v2/topics/"+topicId+"/answer");

        post.addHeader("cookie",cookie);
        post.addHeader("Content-Type","application/json, text/plain, */*");
        post.addHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0");

        AnswerReq answerReq =new AnswerReq(new ReqData(text,silenced));
        String paramJson = JSONObject.fromObject(answerReq).toString();

        StringEntity stringEntity = new StringEntity(paramJson, ContentType.create("text/json","UTF-8"));

        post.setEntity(stringEntity);



        CloseableHttpResponse response = httpClient.execute(post);

        if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
            String jsonStr = EntityUtils.toString(response.getEntity());
            AnswerRes answerRes = JSON.parseObject(jsonStr, AnswerRes.class);
            logger.info("回答问题的结果:groupid:{},topicid:{},jsonstr:{}",groupId,topicId,jsonStr);
            return answerRes.isSucceeded();
        }else{
            throw new RuntimeException("answer err code is "+response.getStatusLine().getStatusCode());
        }
    }
}
